<?php

namespace Common\Models\Videos;

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class VideoGroup extends Model
{
    protected $id;

    protected $slug;

    protected $imdb;

    protected $total_episode;

    protected $is_vip;

    protected $hot_month;

    protected $total_view;

    protected $ip_address;

    protected $created_user;

    protected $published_user;

    protected $created_time;

    protected $updated_time;

    protected $status;

    protected $urlDetail = 'phim';
    public function getSource()
    {
        return 'video_group';
    }

    public function initialize()
    {
        $this->setSource('video_group');

        $this->hasMany('id' , 'Common\Models\Videos\VideoGroupLink' , 'video_group_id' , array(
            'alias' => 'videoGroupLink'
        ));

        $this->hasOne('national_id' , 'Common\Models\National' , 'id' , array(
            'alias' => 'national'
        ));

        $this->hasOne('id' , 'Common\Models\Videos\VideoDescription' , 'video_group_id' , array(
            'alias' => 'videoDescription'
        ));

        $this->hasMany('id' , 'Common\Models\Videos\VideoCategoryLink' , 'video_group_id' , array(
            'alias' => 'videoCategoryLink'
        ));

        $this->hasMany('id' , 'Common\Models\Images\ImageLink' , 'object_id' , array(
            'alias' => 'imageLink'
        ));
    }

    /**
     * @return User
     */
    public static function findFirstById($parameters = array())
    {
        return parent::findFirstById($parameters);
    }

    /**
     * Validate that emails are unique across users
     */
    public function validation()
    {
        
        $this->validate(new Uniqueness(array(
            "field" => "slug",
            "message" => "The slug is already registered"
        )));

        return $this->validationHasFailed() != true;
    }

    public function beforeDelete()
    {
        $videoGroupLink = VideoGroupLink::find(array(
            "video_group_id = " . $this->id
        ));
        if($videoGroupLink)
        {
            $videoGroupLink->delete();
        }

        $videoCategoryLink = VideoCategoryLink::find(array(
            "video_group_id = " . $this->id
        ));
        if($videoCategoryLink)
        {
            $videoCategoryLink->delete();
        }
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param mixed $slug
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
    }

    /**
     * @return mixed
     */
    public function getSlug()
    {
        return $this->slug;
    }


    /**
     * @return mixed
     */
    public function getCreatedTime()
    {
        return $this->created_time;
    }

    /**
     * @param mixed $created_time
     */
    public function setCreatedTime($created_time)
    {
        $this->created_time = $created_time;
    }

    /**
     * @return mixed
     */
    public function getCreatedUser()
    {
        return $this->created_user;
    }

    /**
     * @param mixed $created_user
     */
    public function setCreatedUser($created_user)
    {
        $this->created_user = $created_user;
    }

    /**
     * @return mixed
     */
    public function getHotMonth()
    {
        return $this->hot_month;
    }

    /**
     * @param mixed $hot_month
     */
    public function setHotMonth($hot_month)
    {
        $this->hot_month = $hot_month;
    }

    /**
     * @return mixed
     */
    public function getImdb()
    {
        return $this->imdb;
    }

    /**
     * @param mixed $imdb
     */
    public function setImdb($imdb)
    {
        $this->imdb = $imdb;
    }

    /**
     * @return mixed
     */
    public function getIpAddress()
    {
        return $this->ip_address;
    }

    /**
     * @param mixed $ip_address
     */
    public function setIpAddress($ip_address)
    {
        $this->ip_address = $ip_address;
    }

    /**
     * @return mixed
     */
    public function getIsVip()
    {
        return $this->is_vip;
    }

    /**
     * @param mixed $is_vip
     */
    public function setIsVip($is_vip)
    {
        $this->is_vip = $is_vip;
    }

    /**
     * @return mixed
     */
    public function getPublishedUser()
    {
        return $this->published_user;
    }

    /**
     * @param mixed $published_user
     */
    public function setPublishedUser($published_user)
    {
        $this->published_user = $published_user;
    }

    /**
     * @return mixed
     */
    public function getTotalView()
    {
        return $this->total_view;
    }

    /**
     * @param mixed $total_view
     */
    public function setTotalView($total_view)
    {
        $this->total_view = $total_view;
    }

    /**
     * @return mixed
     */
    public function getUpdatedTime()
    {
        return $this->updated_time;
    }

    /**
     * @param mixed $updated_time
     */
    public function setUpdatedTime($updated_time)
    {
        $this->updated_time = $updated_time;
    }



    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $total_episode
     */
    public function setTotalEpisode($total_episode)
    {
        $this->total_episode = $total_episode;
    }

    /**
     * @return mixed
     */
    public function getTotalEpisode()
    {
        return $this->total_episode;
    }

    public function clearCache()
    {
        if ($this->id) {
            $viewCache = $this->getDI()->getViewCache();
            $viewCache->delete('video-group-' . $this->id);            
        }
    }

    public function getPosterImages()
    {
        $imagesPoster = $this->imageLink->filter(function($imageLink){
            if($imageLink->getType() == 'C')
            {
                return $imageLink;
            }
        });
        if($imagesPoster)
        {
            return $imagesPoster[0];
        }
        return null;
    }

    public function getMainImages()
    {
        $imagesPoster = $this->imageLink->filter(function($imageLink){
            if($imageLink->getType() == 'M')
            {
                return $imageLink;
            }
        });
        if($imagesPoster)
        {
            return $imagesPoster[0];
        }
        return null;
    }

    public function getLinkTo()
    {
        return $this->urlDetail . '/' . $this->slug;
    }   

    public function queryBuilderSeachVideo($conditions)
    {
        $sqlWhere = '1 = 1';
        $conditionsWhere = array();

        if(isset($conditions['status']) && $conditions['status'])
        {
            $sqlWhere .= ' AND videoGroup.status = :status:';
            $conditionsWhere['status'] = $conditions['status'];
        }

        if(isset($conditions['id']) && $conditions['id'])
        {
            $sqlWhere .= ' AND videoGroup.id = :id:';
            $conditionsWhere['id'] = $conditions['id'];
        }

        if(isset($conditions['name']) && trim($conditions['name']))
        {
            $sqlWhere .= ' AND videoDescription.name LIKE :name:';
            $conditionsWhere['name'] = '%' . $conditions['name'] . '%';
        }

        if(isset($conditions['category_id']) && $conditions['category_id'] > 0)
        {
            $sqlWhere .= ' AND videoCategoryLink.category_id = :category_id:';
            $conditionsWhere['category_id'] = $conditions['category_id'];
        }

        if(isset($conditions['national_id']) && $conditions['national_id'] > 0)
        {
            $sqlWhere .= ' AND videoDescription.national_id = :national_id:';
            $conditionsWhere['national_id'] = $conditions['national_id'];
        }

        $videosGroups = $this->getDI()->getModelsManager()->createBuilder()
            ->from(array('videoGroup' => 'Common\Models\Videos\VideoGroup'))
            ->groupBy("videoGroup.id")
            ->leftJoin('Common\Models\Videos\VideoDescription' , 'videoDescription.video_group_id = videoGroup.id' , 'videoDescription')
            ->leftJoin('Common\Models\Videos\VideoCategoryLink' , 'videoCategoryLink.video_group_id = videoGroup.id' , 'videoCategoryLink')
            ->where($sqlWhere , $conditionsWhere)
            ->orderBy('videoGroup.id');
        return $videosGroups;
    }

}
